#include "key.h"
unsigned char keynum = 255;
sbit row3 = P3^2;
sbit row4 = P3^3;
sbit col1 = P4^4;
sbit col2 = P4^2;
sbit col3 = P3^5;

void clean_row(void)
{
	row3 = 1;
	row4 = 1;
	col1 = 0;
	col2 = 0;
	col3 = 0;
}
void clean_col(void)
{
	row3 = 0;
	row4 = 0;
	col1 = 1;
	col2 = 1;
	col3 = 1;
}

void scankey(void)
{
	static unsigned char status = 0;
	static unsigned char midvalue = 0;
	switch(status)
	{
		case 0:
			clean_row();
			if(row3 == 0 || row4 == 0)
			{
				status = 1;
			}
			break;
		case 1:
			clean_row();
			if(row3 == 0)
			{
				status = 2;
				midvalue = 1;
			}
			else if(row4 == 0)
			{
				status = 2;
				midvalue = 0;
			}
			else
			{
				status = 0;
			}
		break;
		case 2:
			clean_col();
			if(col1 == 0)
			{
				status = 3;
				midvalue = 4 + midvalue;
			}
			else if(col2 == 0)
			{
				status = 3;
				midvalue = 8 + midvalue;
			}
			else if(col3 == 0)
			{
				status = 3;
				midvalue = 12 + midvalue;
			}
			else
			{
				status = 0;
			}
			break;
		case 3:
			clean_col();
			if(col1 == 1&& col2 == 1 && col3 == 1)
			{
				keynum = midvalue;
				status = 0;
			}
			break;
	}
}